package com.web;

import com.javabean.User;
import com.service.impl.UserServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;


public class UserServlet extends BaseServlet {

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserServiceImpl a = new UserServiceImpl();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (a.existUsername(username)) {
            User loginuser = a.login(username, password);
            if (loginuser != null) {
                //免用户名登录
                Cookie cookie = new Cookie("username", username);
                cookie.setMaxAge(60 * 60 * 24 * 7);//当前cookie一周内有效
                response.addCookie(cookie);
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                session.setAttribute("user", loginuser);
                request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
            } else {
                request.setAttribute("username", username);
                request.setAttribute("msg", "用户名或密码错误！");
                request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("username", username);
            request.setAttribute("msg", "用户名或密码错误！");
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }
    }

    public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserServiceImpl a = new UserServiceImpl();
        //获取请求的参数信息
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        User user = WebUtils.copyParamToBean(request.getParameterMap(), new User());
        //获取Session中的验证码
        Object attribute = request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除验证码
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (code.equalsIgnoreCase((String) attribute)) {
            //检查用户名是否存在
            if (a.existUsername(username)) {
                request.setAttribute("username", username);
                request.setAttribute("email", email);
                request.setAttribute("msg", "用户名已存在");
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                a.registerUser(user);
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }
        } else {
            //请求转发
            request.setAttribute("username", username);
            request.setAttribute("email", email);
            request.setAttribute("msg", "验证码错误！");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }


    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();//销毁session域中的内容
        response.sendRedirect(request.getContextPath());

    }
}
